package com.cloud.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cloud.common.result.PageResult;
import com.cloud.common.result.Result;
import com.cloud.model.form.PaymentForm;
import com.cloud.model.query.PaymentPageQuery;
import com.cloud.model.vo.PaymentPageVO;
import com.cloud.service.PaymentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 支付表 前端控制器
 *
 * @author Sailer Wen
 * @since 2024-07-24
 */
@Tag(name = "支付表接口")
@RestController
@RequestMapping("/payment")
@RequiredArgsConstructor
public class PaymentController {

    @Value("${server.port}")
    private String server_port;

    private final PaymentService paymentService;

    // 服务发现
    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("discovery")
    public Object discovery() {
        List<String> services = discoveryClient.getServices();
        services.forEach(System.out::println);

        for (String service : services){
            List<ServiceInstance> instances = discoveryClient.getInstances(service);
            instances.forEach(instance -> {
                System.out.println("instance:"+instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t" + instance.getUri());
            });
        }
        return this.discoveryClient;
    }

    @Operation(summary = "支付表分页列表")
    @GetMapping("/page")
    public PageResult<PaymentPageVO> listPagedPayments(PaymentPageQuery queryParams ) {
        IPage<PaymentPageVO> result = paymentService.listPagedPayments(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增支付表")
    @PostMapping
    public Result savePayment(@RequestBody PaymentForm formData ) {
        boolean result = paymentService.savePayment(formData);

        if(result){
            return Result.success(server_port+" ：新增支付成功！");
        }else {
            return Result.failed("新增支付失败！");
        }

    }

    @Operation(summary = "支付表表单数据")
    @GetMapping("/{id}")
    public Result getPaymentForm(
        @Parameter(description = "支付表ID") @PathVariable Long id
    ) {
        PaymentForm formData = paymentService.getPaymentFormData(id);
        return Result.success(server_port, formData);
    }

    @Operation(summary = "修改支付表")
    @PutMapping(value = "/{id}")
    public Result updatePayment(@Parameter(description = "支付表ID") @PathVariable Long id,
    @RequestBody @Validated PaymentForm formData) {
        boolean result = paymentService.updatePayment(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除支付表")
    @DeleteMapping("/{ids}")
    public Result deletePayments(
        @Parameter(description = "支付表ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = paymentService.deletePayments(ids);
        return Result.judge(result);
    }

    @GetMapping("lb")
    public String getPaymentLB() {
        return server_port;
    }

    @GetMapping("feign/timeout")
    public String getFeignTimeOut() {
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return server_port;
    }
}
